package com.bkgy.controller.driver;

import com.bkgy.controller.repOutput.service.IRepOutputService;
import com.bkgy.controller.repTcmThickWidth.service.IRepTcmThickWidthService;
import com.bkgy.controller.tPdiDataL3Tcm2280.service.IRepTcmOutputHourService;
import com.bkgy.controller.tcmReport.service.IRepTcmOutputInfService;
import com.bkgy.controller.tcmReport.service.IRepTcmOutputPlanActService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.DateUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Date;

@Tag(name = "基础数据构建")
@Controller
@RequestMapping("/driver/tcmOutput")
@RequiredArgsConstructor
public class TcmOutputController {
    private final IRepOutputService repOutputService;
    private final IRepTcmOutputPlanActService repPlanActervice;
    private final IRepTcmOutputInfService repInfService;
    private final IRepTcmThickWidthService repThickWidthService;
    private final IRepTcmOutputHourService repHourService;

    @ResponseBody
    @Operation(summary = "构建截止今日零时各机组产量数据，初始化表用", description = "REP_OUTPUT")
    @PostMapping("/REP_OUTPUT/initRepOutput0")
    public ResultData<String> initRepOutput0() {
        //构建截止今日零时数据，初始化表用
        repOutputService.initRepOutput0();

        repOutputService.initRepOutput1();
        return ResultData.ResultDataSuccess("SUCCESS");
    }

    @ResponseBody
    @Operation(summary = "构建产量班产、日产数据", description = "REP_OUTPUT")
    @PostMapping("/REP_OUTPUT/initRepOutput1")
    public ResultData<String> initRepOutput1() {
        repOutputService.initRepOutput1();
        return ResultData.ResultDataSuccess("SUCCESS");
    }

    @Operation(summary = "更新REP_OUTPUT年产、月产列")
    @GetMapping("/REP_OUTPUT/updateOutputYearMonthJob")
    public ResultData<String> updateOutputYearMonthJob() {
        //前一天日期
        Date d = DateUtil.getBeforDate();
        repOutputService.updateOutputYearMonthJob(d);
        return ResultData.ResultDataSuccess("SUCCESS");
    }

    @Operation(summary = "酸轧生产效率，产量计划、下达、实际基础数据", description = "日计划下达量与日计划产量达成率柱形图基础数据")
    @PostMapping("/REP_TCM_OUTPUT_PLAN_ACT/buildOutputPlanAct")
    @ResponseBody
    public ResultData buildOutputPlanAct(String startDay, String endDay) {
        Date d1 = DateUtil.getStringToDate("yyyyMMdd", startDay);
        Date d2 = DateUtil.getStringToDate("yyyyMMdd", endDay);

        while (d1.before(d2)) {
            repPlanActervice.initDayPlanXiadaJob(d1);
            d1 = DateUtil.getAfterDate(d1);
        }
        return ResultData.ResultDataSuccess("SUCCESS");
    }

    @Operation(summary = "计算每日的实际产量、废次降", description = "计算每日的实际产量、废次降")
    @PostMapping("/REP_TCM_OUTPUT_PLAN_ACT/buildFeicijiang")
    @ResponseBody
    public ResultData buildFeicijiang(String startDay, String endDay) {
        Date d1 = DateUtil.getStringToDate("yyyyMMdd", startDay);
        Date d2 = DateUtil.getStringToDate("yyyyMMdd", endDay);

        while (d1.before(d2)) {
            repPlanActervice.updateLastDayOutputJob(d1);
            d1 = DateUtil.getAfterDate(d1);
        }
        return ResultData.ResultDataSuccess("SUCCESS");
    }

    @Operation(summary = "构建酸轧班组报表总览》影响产量列表，表：REP_TCM_OUTPUT_INF", description = "影响产量列表")
    @PostMapping("/REP_TCM_OUTPUT_INF/buildInfOutput")
    @ResponseBody
    public ResultData buildInfOutput(String startDay, String endDay) {
        try {
            Date d1 = DateUtil.getStringToDate("yyyyMMdd", startDay);
            Date d2 = DateUtil.getStringToDate("yyyyMMdd", endDay);

            while (d1.before(d2)) {
                repInfService.buildInfOutputJob(d1);
                d1 = DateUtil.getAfterDate(d1);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataSuccess(e.getMessage());
        }
        return ResultData.ResultDataSuccess("SUCCESS");
    }

    @Operation(summary = "构建钢种宽度厚度分析", description = "生产效益手工更新数据 yyyyMMdd")
    @GetMapping("/REP_TCM_THICK_WIDTH/buildSteelGradeWidthThick")
    public ResultData<String> buildSteelGradeWidthThick(String beginDate, String endDate) {
        Date d1 = DateUtil.getStringToDate("yyyyMMdd", beginDate);
        Date d2 = DateUtil.getStringToDate("yyyyMMdd", endDate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(d1);
        while (d1.before(d2)) {
            repThickWidthService.buildSteelGradeWidthThickJob(d1);
            cal.add(Calendar.DAY_OF_MONTH, 1);
            d1 = cal.getTime();
        }
        return ResultData.ResultDataSuccess("SUCCESS");
    }

    @Operation(summary = "构建酸轧产量报表数据", description = "从传入第一个时间的前一个小时开始构建，2025092501-2025092601")
    @GetMapping("/REP_TCM_OUTPUT_HOUR/buildTcm2280OutputRep")
    @ResponseBody
    public ResultData<String> buildTcm2280OutputRep(String startDay, String endDay) {
        try {
            Date d1 = DateUtil.getStringToDate("yyyyMMddHH", startDay);
            Date d2 = DateUtil.getStringToDate("yyyyMMddHH", endDay);
            if (d1 == null || d2 == null) {
                throw new RuntimeException("时间格式错误");
            }

            while (d1.before(d2)) {
                repHourService.buildOutputBeforeHour(d1);
                d1 = DateUtil.getAfterHour(d1);
            }
            repHourService.currentHourOutput();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataSuccess(e.getMessage());
        }
        return ResultData.ResultDataSuccess("success");
    }

    @Operation(summary = "构建酸轧产量报表当日计划数据")
    @GetMapping("/REP_TCM_OUTPUT_HOUR/buildTcm2280OutputDayRep")
    @ResponseBody
    public ResultData<String> buildTcm2280OutputDayRep() {
        repHourService.buildOutput2280Day();
        return ResultData.ResultDataSuccess("success");
    }
}
